package com.dianneng.show.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.dianneng.show.R;
import com.dianneng.show.base.BaseActivity;
import com.dianneng.show.http.RetrofitHelper;
import com.dianneng.show.model.DeviceListInfo;
import com.dianneng.show.model.LastLightDataInfo;
import com.dianneng.show.model.ProjectListInfo;
import com.dianneng.show.util.ACache;
import com.dianneng.show.util.Constant;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        int dpi=ScreenUtils.getScreenDensityDpi();
        float d=ScreenUtils.getScreenDensity();

        LogUtils.i("dpi="+dpi+" density="+d);
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        login();
    }

    private void login(){
        if (TextUtils.isEmpty(etUsername.getText().toString())){
            ToastUtils.showShort("用户名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString())){
            ToastUtils.showShort("密码不能为空！");
            return;
        }
        mLoadingDialog.setLoadingText("登录中...").show();
        projectlist();
    }

    private void projectlist(){
        disposables.add(RetrofitHelper.getHelper(getApplicationContext()).getUserInfoService().projectlist()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ProjectListInfo>() {
                    @Override
                    public void accept(ProjectListInfo info) {
                        if (info!=null && !info.getData().isEmpty()){
                            ACache.get(Utils.getApp()).put(Constant.ID,info.getData().get(0).getId()+"");
                            ACache.get(Utils.getApp()).put(Constant.ADDRESS,info.getData().get(0).getAddress());
                            ACache.get(Utils.getApp()).put(Constant.NAME,info.getData().get(0).getName());
                            devicelist();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        throwable.printStackTrace();
                        mLoadingDialog.close();
                    }
                })
        );
    }

    private void devicelist(){
        disposables.add(RetrofitHelper.getHelper(getApplicationContext()).getUserInfoService().devicelist(ACache.get(Utils.getApp()).getAsString(Constant.ID))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<DeviceListInfo>() {
                    @Override
                    public void accept(DeviceListInfo info) {
                        if (info!=null && !info.getData().isEmpty()){
                            ACache.get(Utils.getApp()).put(Constant.SN,info.getData().get(0).getSn());
                            ACache.get(Utils.getApp()).put(Constant.DEVICE_SN,info.getData().get(0).getDevicesn());
                            ACache.get(Utils.getApp()).put(Constant.LOCATION,info.getData().get(0).getLocation());
                            ACache.get(Utils.getApp()).put(Constant.CREATE_TIME,info.getData().get(0).getCreatetime());
                            lastLightData();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        throwable.printStackTrace();
                        mLoadingDialog.close();
                    }
                })
        );
    }

    private void lastLightData(){
        disposables.add(RetrofitHelper.getHelper(getApplicationContext()).getUserInfoService().lastLightData(ACache.get(Utils.getApp()).getAsString(Constant.SN))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<LastLightDataInfo>() {
                    @Override
                    public void accept(LastLightDataInfo info) {
                        if (info!=null && !info.getData().isEmpty()){
                            ACache.get(Utils.getApp()).put(Constant.EQ,info.getData().get(0).getEq()+"");
                            ACache.get(Utils.getApp()).put(Constant.TIME,info.getData().get(0).getCreatetime());
                            ACache.get(Utils.getApp()).put(Constant.TEMP_SN,info.getData().get(0).getSn());

                            ActivityUtils.startActivity(new Intent(LoginActivity.this,Main2Activity.class));
                            ActivityUtils.finishActivity(LoginActivity.this,true);
                            ToastUtils.showShort("登录成功");
                        }else {
                            ToastUtils.showShort("登录失败");
                        }
                        mLoadingDialog.close();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        throwable.printStackTrace();
                        mLoadingDialog.close();
                    }
                })
        );
    }
}
